package FlowGram.Application.User;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.Scanner;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;


public class Login extends JPanel implements ActionListener{
	private static String Signin = "Login";

	
	private JFrame control; 
	private JPasswordField passField;
	private JTextField textField; 
	private UserManager log; 
	
	public Login (JFrame f){
		control = f; 
		passField = new JPasswordField(10);
		passField.setActionCommand(Signin);
		passField.addActionListener(this);
		
		textField = new JTextField(20);
		textField.setActionCommand(Signin);
		textField.addActionListener(this);
		
		
		JLabel label = new JLabel("Enter the Password:");
		label.setLabelFor(passField);
		JLabel tlabel = new JLabel("Enter the Username:");
		tlabel.setLabelFor(textField); 
		
		// This will hold all the buttons 
		JComponent buttonPane = createButtonPanel();
		
		// Lay out everything 
		JPanel textPane = new JPanel();
		textPane.setLayout(new BoxLayout(textPane, BoxLayout.Y_AXIS ));
		textPane.add(tlabel);
		textPane.add(textField); 
		textPane.add(label);
		textPane.add(passField); 
		
		
		//Will add to the frame 
		add(textPane);
		add(buttonPane); 
		
		//the userManager
		log = new UserManager(); 
		
	}
	/**
	 * Where the button are arranged
	 * @return
	 */
	protected JComponent createButtonPanel(){
		JPanel p = new JPanel(new GridLayout(0,1));
		JButton userButton = new JButton("Login");	
       userButton.setActionCommand(Signin);       
       userButton.addActionListener(this);  
       
       p.add(userButton);
       
       return p; 
	}
	
	  public void actionPerformed(ActionEvent e) {
	        String cmd = e.getActionCommand();

	        if (Signin.equals(cmd)) { // Process the user name 
	        	//  Want to get the info from the textfield 
	            //char[] input = passField.getPassword();
	            @SuppressWarnings("deprecation")
				String pass = passField.getText();
	            String user = textField.getText(); 
	            try {
					if (log.login(user, pass)) { // Method to check if the password is correct  
					    JOptionPane.showMessageDialog(control,
					        "Success! You typed the right user info");
					    	System.exit(0); 
					} else {
					    JOptionPane.showMessageDialog(control,
					        "Invalid Login Information Try again.",
					        "Error Message",
					        JOptionPane.ERROR_MESSAGE);
					}
				} catch (HeadlessException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
	            

	            //Zero out the possible password, for security.
	           // Arrays.fill(input, '0');

	            textField.selectAll();
	            resetFocus(); // this method does ?
	        }
	        
	    }
	  
	  
	  //Must be called from the event dispatch thread.
	    protected void resetFocus() {
	        textField.requestFocusInWindow();
	    }
	    
	    /**
	     * Create the GUI and show it.  For thread safety,
	     * this method should be invoked from the
	     * event dispatch thread.
	     */
	    private static void createAndShowGUI() {
	        //Create and set up the window.
	        JFrame frame = new JFrame("Login");
	        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

	        //Create and set up the content pane.
	        final Login newContentPane = new Login(frame);
	        newContentPane.setOpaque(true); //content panes must be opaque
	        frame.setContentPane(newContentPane);

	        //Make sure the focus goes to the right component
	        //whenever the frame is initially given the focus.
	        frame.addWindowListener(new WindowAdapter() {
	            public void windowActivated(WindowEvent e) {
	                newContentPane.resetFocus();
	            }
	        });

	        //Display the window.
	        frame.pack();
	        frame.setVisible(true);
	    }

	    public static void main(String[] args) {
	        //Schedule a job for the event dispatch thread:
	        //creating and showing this application's GUI.
	        SwingUtilities.invokeLater(new Runnable() {
	            public void run() {
	                //Turn off metal's use of bold fonts
			UIManager.put("swing.boldMetal", Boolean.FALSE);
			createAndShowGUI();
	            }
	        });
	    }
	    

}